<template>
  <div class="admin-appointment-container">
    <h2 class="section-title">预约管理</h2>
    <div style="margin-bottom: 12px;">
      <el-input v-model="search" placeholder="请输入用户名/咨询师名" style="width: 200px; margin-right: 8px;" clearable />
      <el-button type="primary" @click="onSearch">查询</el-button>
      <el-button @click="resetSearch">重置</el-button>
      <el-button type="primary" @click="addDialogVisible = true" style="float:right">添加预约</el-button>
    </div>
    <el-table :data="filteredList" style="width: 100%" border stripe highlight-current-row>
      <el-table-column label="预约ID" prop="id" align="center" width="80"></el-table-column>
      <el-table-column label="用户" prop="username" align="center"></el-table-column>
      <el-table-column label="咨询师" prop="consultantName" align="center"></el-table-column>
      <el-table-column label="预约时间" prop="appointmentTime" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button size="small" @click="editRow(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteRow(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加/编辑对话框 -->
    <el-dialog v-model="addDialogVisible" :title="editMode ? '编辑预约' : '添加预约'" width="400px">
      <el-form :model="form" label-width="80px">
        <el-form-item label="用户">
          <el-select v-model="form.userId" placeholder="请选择用户">
            <el-option v-for="u in userList" :key="u.id" :label="u.username" :value="u.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="咨询师">
          <el-select v-model="form.consultantId" placeholder="请选择咨询师">
            <el-option v-for="c in consultantList" :key="c.id" :label="c.username" :value="c.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="预约时间">
          <el-date-picker v-model="form.appointmentTime" type="datetime" placeholder="选择预约时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%;" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const appointmentList = ref([]);
const userList = ref([]);
const consultantList = ref([]);
const search = ref('');
const addDialogVisible = ref(false);
const editMode = ref(false);
const form = ref({ id: '', userId: '', consultantId: '', appointmentTime: '' });

const filteredList = computed(() => {
  if (!search.value) return appointmentList.value;
  return appointmentList.value.filter(item =>
    (item.username && item.username.includes(search.value)) ||
    (item.consultantName && item.consultantName.includes(search.value))
  );
});

const onSearch = () => {};
const resetSearch = () => { search.value = ''; };

const getAppointmentList = async () => {
  const res = await axios.get('/api/appointment/list.do');
  appointmentList.value = res.data.data || [];
};
const getUserList = async () => {
  const res = await axios.get('/api/user/list.do');
  userList.value = res.data.data || [];
};
const getConsultantList = async () => {
  const res = await axios.get('/api/consultant/list.do');
  consultantList.value = res.data.data || [];
};

onMounted(() => {
  getAppointmentList();
  getUserList();
  getConsultantList();
});

const editRow = (row) => {
  form.value = { ...row, userId: row.userId, consultantId: row.consultantId };
  editMode.value = true;
  addDialogVisible.value = true;
};
const deleteRow = async (id) => {
  const params = new URLSearchParams();
  params.append('id', id);
  const res = await axios.post('/api/appointment/delete.do', params, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
  if (res.data.code === 200) {
    ElMessage.success('删除成功');
    getAppointmentList();
  } else {
    ElMessage.error(res.data.message || '删除失败');
  }
};
const onSubmit = async () => {
  if (!form.value.userId || !form.value.consultantId || !form.value.appointmentTime) {
    ElMessage.error('请填写完整信息');
    return;
  }
  const params = new URLSearchParams();
  params.append('userId', form.value.userId);
  params.append('consultantId', form.value.consultantId);
  params.append('appointmentTime', form.value.appointmentTime);
  if (editMode.value) {
    params.append('id', form.value.id);
    const res = await axios.post('/api/appointment/update.do', params, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
    if (res.data.code === 200) {
      ElMessage.success('修改成功');
      addDialogVisible.value = false;
      getAppointmentList();
    } else {
      ElMessage.error(res.data.message || '修改失败');
    }
  } else {
    const res = await axios.post('/api/appointment/insert.do', params, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
    if (res.data.code === 200) {
      ElMessage.success('添加成功');
      addDialogVisible.value = false;
      getAppointmentList();
    } else {
      ElMessage.error(res.data.message || '添加失败');
    }
  }
  editMode.value = false;
  form.value = { id: '', userId: '', consultantId: '', appointmentTime: '' };
};
</script>

<style scoped>
.admin-appointment-container {
  max-width: 900px;
  margin: 30px auto;
  background: #fff;
  padding: 32px 24px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.05);
}
.section-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 16px;
}
</style> 